//
//  CircleCollectionView.m
//  QQMV
//
//  Created by yh on 16/8/13.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "CircleCollectionView.h"
#define kContentOffSize [UIScreen mainScreen].bounds.size.width - 10
@implementation CircleCollectionView
- (instancetype)initWithFrame:(CGRect)frame
                   imageArray:(NSArray *)imageArray {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        _imgArray = [NSMutableArray arrayWithArray:imageArray];
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
        flow.itemSize = CGSizeMake(frame.size.width, frame.size.height);
        // 最小列间距一定要设置为0
        flow.minimumLineSpacing = 0;
        flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collect = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flow];
        _collect.pagingEnabled = YES;
        [self addSubview:_collect];
        _collect.delegate = self;
        _collect.dataSource = self;
        [_collect registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
        // 首尾
        [_imgArray insertObject:[imageArray lastObject] atIndex:0];
        [_imgArray addObject:[imageArray firstObject]];
        // 初始偏移量 !!!!
        _collect.contentOffset = CGPointMake(kContentOffSize, 0);
        // 定时器
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerChangePageAction) userInfo:nil repeats:YES];
        // page
        _page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, _imgArray.count * 30, 30)];
        _page.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height * 7 / 8);
        [self addSubview:_page];
        _page.numberOfPages = _imgArray.count - 2;
        _page.currentPageIndicatorTintColor = [UIColor redColor];
        _page.pageIndicatorTintColor = [UIColor whiteColor];
        [_page addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
    
    }
    return self;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.timer.fireDate = [NSDate distantFuture];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    self.timer.fireDate = [NSDate distantPast];
}
- (void)timerChangePageAction {
    if (_page.currentPage == _page.numberOfPages - 1) {
        _page.currentPage = 0;
    } else {
        _page.currentPage++;
    }
    _collect.contentOffset = CGPointMake((_page.currentPage + 1 ) * _collect.bounds.size.width , 0);
}
- (void)pageChange:(UIPageControl *)page {
    _collect.contentOffset = CGPointMake((_page.currentPage + 1 ) * _collect.bounds.size.width, 0);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _imgArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    // 触发方法
    [self.dataSource handleImageView:cell objc:_imgArray[indexPath.row]];
    return cell;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (_collect.contentOffset.x == 0) {
        [_collect scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:_imgArray.count - 2 inSection:0] atScrollPosition:0 animated:NO];
    } else if (_collect.contentOffset.x == (_imgArray.count - 1 ) * _collect.bounds.size.width) {
        [_collect scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:0 animated:NO];
    }
    _page.currentPage = _collect.contentOffset.x / _collect.bounds.size.width - 1;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end



@implementation CustomCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self.contentView addSubview:_imageView];
        _imageView.backgroundColor = [UIColor blueColor];
    }
    return self;
}



@end
